<template>
  <div>
    <!-- 最新文章 -->
    <div class="main" v-for="(item,index) in postList" :key="index" @click="toPostDetail(item)">
      <div class="imgs">
        <img :src="item.images" alt />
      </div>
      <div class="content">
        <div class="title">{{item.title}}</div>
        <div class="date">
          <span style="margin-right: 10px;">{{getDate(item.created_at)}}</span>
          <span>
            <span style="margin-right: 5px;">阅读</span>
            {{item.watch}}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// 引入momentjs
import moment from "moment";
export default {
  data() {
    return {
      // 存放文章列表
      postList: [{ date: "" }],
      //   存放时间
      dateList: []
    };
  },
  mounted() {
    this.$axios({
      url: "/posts/recommend"
    }).then(res => {
      const { data } = res.data;
      // console.log(data);
      this.postList = data;
      // 调用父组件转换日期方法
      //   this.$emit('getDate',data)
    });
  },
  methods: {
    toPostDetail(data) {
      // 获取文章id
      const { id } = data;
      this.$router.push({
        path: "/post/detail",
        query: {
          id
        }
      });
    },
    getDate(time) {
      let date = new Date(time);
      date = moment(date).format("YYYY-MM-DD hh:mm");
      return date;
    }
  }
};
</script>

<style lang="less" scoped>
.main {
  padding: 20px 0;
  display: flex;
  justify-content: flex-start;
  border-bottom: 1px solid #ddd;
  .imgs {
    width: 100px;
    height: 80px;
    background: #ddd;
    overflow: hidden;
    margin-right: 10px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .content {
    .title {
      height: 60px;
    }
    .date {
      font-size: 12px;
      color: #999;
    }
  }
}
</style>